home *** CD-ROM | disk | FTP | other *** search
Text File | 1988-07-21 | 33.6 KB | 1,073 lines | [TEXT/MACA] |
- PROGRAM Viewer;
-
- {--------------------- A Viewer for PixelPaint 1.0 ---------------------}
- {--------------------- Written by Keith McGreggor ---------------------}
- {--------------------- (c) 1987 by Pixel Resources, Inc. --------------}
- {--------------------- Commercial Rights Reserved ---------------------}
- {--------------------- -------------------------- ---------------------}
- {------ It is my intention that this evolve into a general graphic -----}
- {------ file conversion utility. You may modify this code in any -----}
- {------ way you want, as long as you then upload the application -----}
- {------ and source code for other authors to contribute to and to -----}
- {------ learn from. In this way, we can each explore other file -----}
- {------ formats, and (eventually) come up with a single color file -----}
- {------ specification. -----}
- {------ --- Keith McGreggor -----}
- {-----------------------------------------------------------------------}
-
- USES
- Memtypes,Quickdraw,Osintf,Toolintf,Packintf;
-
- PROCEDURE _DataInit; EXTERNAL;
-
- {------------------------------------------------------------------------------}
-
- TYPE
- ColorMapType = ARRAY[ 0..255 ] OF ColorSpec;
- TextureMapType = ARRAY [ 0..143 ] OF Pattern;
- ViewRecord = RECORD
- ViewOpen : Boolean; { is the Window open }
- FullView : Boolean; { is the whold document visible }
- WindowType : Integer; { 5-Scollable 6-NoGrow 7-NoTitleNoGrow }
- WindowPortRect : Rect; { PortRect of Window }
- ReadOnlyWindow : Boolean; { true if window is on low-res screen }
- DocScreenRect : Rect; { Window Area containing the Document }
- DeskZoomRect : Rect; { StdState in window's ZoomRect }
- DeskUserRect : Rect; { UserState in window's ZoomRect }
- AnchorRect : Rect; { area of Document showing }
- ZoomMode : Integer; { what level of fatbits }
- Zoomanchor : Point; { future use }
- Splitlocation : Integer; { future use }
- RulerOrigin : Point; { origin of rulers }
- RulerUnits : Integer; { pixels, inches, metric, picas }
- RulersOpen : Boolean; { are rulers showing }
- HeightScrollBar : ControlHandle; { handle to Horz Scroll Bars }
- WidthScrollBar : ControlHandle; { handle to Vert Scroll Bars }
- ViewPtr : WindowPtr; { WindowPtr }
- LinkUp : Integer; { future use }
- fatrect : Rect; { window coords of fatbits rect }
- reserved : ARRAY [1..2] OF LongInt; { future use}
- END;
- FileHeaderPtr = ^FileHeader;
- FileHeader = RECORD
- Version : LongInt; { file version type (should be MAXINT) }
- PatArray : ARRAY[1..36] OF Pattern; { future use }
- CanvasSize : Integer; { 1-576x720 2-512x512 3-1024x1024 4-1024x768}
- CanvasDepth : Integer; { bits per pixel }
- WindType : Integer; { Default window type }
- ViewsUsed : Integer; { Currently set to 1 }
- ViewArray : ARRAY[1..8] OF ViewRecord;
- OVOpen : Boolean; { Is Overview Open }
- CurrentOV : Integer; { index of current OV windowsel }
- OVPoint : Point; { topleft pt of Overview Window }
- RulerUnit : Integer; { Default Ruler Unit }
- OtherFont : Integer; { font size of Other font }
- GridSize : Integer; { grid size }
- FXButton : Boolean; { true if set for all tools }
- IconMargin : Boolean; { true if leave room for icons }
- MseCrawl : Boolean; { true if not mouse crawl }
- FatbitScroll : Boolean; { true if fatbit autoscroll }
- cornerstretch : Boolean; { true if stretch from corners }
- RemapColors : Boolean; { true if color remapping is on }
- patternopt : Integer; { opcode number for Option Key effects }
- noOfHues : Integer; { number of absolute hues in the palette }
- spraycansize : Integer; { for later use }
- END;
-
- VAR
- applemenu, filemenu, editmenu : MenuHandle;
- watch : CursHandle;
- viewwindowrecord : WindowRecord;
- viewwindow : WindowPtr;
- viewevent : EventRecord;
- userwantstoquit : Boolean;
- horizontalscroll,verticalscroll : ControlHandle;
- draggingarea : Rect;
- documentwidth,documentheight : Integer;
- bufferport : CGrafPtr;
- bufferpix : PixMapHandle;
- docport : CGrafPtr;
- docpix : PixMapHandle;
- docsize : Integer;
- anchorpt,docpt : Point;
- mycolormap : ColorMapType;
-
- {------------------------------------------------------------------------------}
- {$S COLORSTUFF}
- {-----------------------------------------------------------------------------------------}
-
- FUNCTION CreatePixMap( VAR mypixmap : PixMapHandle; width, height, depth : Integer ) : Boolean;
- VAR
- thegdevice : GDHandle;
- myptr : Ptr;
- bitsperrow,bytesperrow,bytesneeded : LongInt;
- BEGIN
- thegdevice := GetGDevice;
- CreatePixMap := false;
- mypixmap := NewPixMap;
- bitsperrow := LongInt( depth * width );
- bytesperrow := bitsperrow div 8;
- IF (odd(bytesperrow)) THEN bytesperrow := bytesperrow + 1;
- bytesneeded := bytesperrow * height;
- myptr := nil;
- myptr := Ptr( NewPtr( bytesneeded ) );
- IF (myptr <> NIL) THEN BEGIN
- mypixmap^^.baseaddr := myptr;
- mypixmap^^.rowbytes := Integer(bytesperrow + 32768);
- SetRect(mypixmap^^.bounds,0,0,width,height);
- mypixmap^^.pmtable := thegdevice^^.gdpmap^^.pmtable;
- mypixmap^^.pixelsize := depth;
- mypixmap^^.cmpsize := depth;
- CreatePixMap := true;
- END
- ELSE CreatePixMap := false;
- END;
-
- {------------------------------------------------------------------------------}
-
- FUNCTION BuildNewPort( VAR myport : CGrafPtr; VAR mypixmap : PixMapHandle;
- width, height, depth : Integer ) : Boolean;
- VAR
- result : Boolean;
- savedport : GrafPtr;
- BEGIN
- GetPort(savedport);
- myport := NIL;
- myport := CGrafPtr( NewPtr( sizeof(CGrafPort) ) );
- result := CreatePixMap( mypixmap, width, height, depth );
- IF ((myport <> NIL) AND result) THEN BEGIN
- OpenCPort(myport);
- SetPort(grafptr(myport));
- myport^.portpixmap := mypixmap;
- PortSize(mypixmap^^.bounds.right,mypixmap^^.bounds.bottom);
- ClipRect(mypixmap^^.bounds);
- myport^.visrgn := myport^.cliprgn;
- EraseRect(myport^.portrect);
- BuildNewPort := true;
- END
- ELSE BuildNewPort := False;
- SetPort(savedport);
- END;
-
- {-----------------------------------------------------------------------------------------}
-
- PROCEDURE PixelSetEntries(start, count: INTEGER; aptr: ptr); INLINE $AA3F;
-
- {-----------------------------------------------------------------------------------------}
-
- PROCEDURE SetColorMap(mymap : ColorMapType);
- VAR
- i : integer;
- wmp : grafptr;
- oldport : grafptr;
- BEGIN
- IF GetMainDevice^^.gdPMap^^.pmtable^^.ctsize <> 255 THEN Exit(SetColorMap);
- mymap[0].rgb.red := $FFFF;
- mymap[0].rgb.blue := $FFFF;
- mymap[0].rgb.green := $FFFF;
- mymap[255].rgb.red := $0000;
- mymap[255].rgb.blue := $0000;
- mymap[255].rgb.green := $0000;
- FOR i := 1 TO 254 DO
- IF (mymap[i].rgb.red = $FFFF) AND (mymap[i].rgb.blue = $FFFF)
- AND (mymap[i].rgb.green = $FFFF) THEN BEGIN
- mymap[i].rgb.red := $FFFE;
- mymap[i].rgb.blue := $FFFE;
- mymap[i].rgb.green := $FFFE;
- END
- ELSE IF (mymap[i].rgb.red = $0000) AND (mymap[i].rgb.blue = $0000)
- AND (mymap[i].rgb.green = $0000) THEN BEGIN
- mymap[i].rgb.red := $0001;
- mymap[i].rgb.blue := $0001;
- mymap[i].rgb.green := $0001;
- END;
- SetGDevice(GetMainDevice);
- FOR i := 0 TO 255 DO BEGIN ProtectEntry(i,false); ReserveEntry(i,false); END;
- PixelSetEntries(0,255,@mymap);
- FOR i := 0 TO 255 DO ProtectEntry(i,true);
- GetPort(oldport);
- IF oldport = viewwindow THEN BEGIN
- GetWMgrPort(wmp);
- IF wmp <> nil THEN PaintOne(nil, wmp^.cliprgn);
- SetPort(viewwindow);
- END;
- END;
-
- {-----------------------------------------------------------------------------------------}
-
- PROCEDURE SetForegroundColor( cv : Integer );
- VAR
- aport : grafptr;
- BEGIN
- getport(aport);
- if (bitand(cgrafptr(aport)^.portversion,$C000) <> $C000) then EXIT(SetForegroundColor);
- rgbforecolor(MyColorMap[cv].rgb);
- IF GetMainDevice^^.gdPMap^^.Pixeltype = 1 THEN EXIT(SetForegroundColor);
- cgrafptr(aport)^.fgcolor := longint(cv);
- END;
-
- {-----------------------------------------------------------------------------------------}
-
- PROCEDURE SetBackgroundColor( cv : Integer );
- VAR
- aport : grafptr;
- BEGIN
- getport(aport);
- if (bitand(cgrafptr(aport)^.portversion,$C000) <> $C000) then EXIT(SetBackgroundColor);
- rgbbackcolor(MyColorMap[cv].rgb);
- IF GetMainDevice^^.gdPMap^^.Pixeltype = 1 THEN EXIT(SetBackgroundColor);
- cgrafptr(aport)^.bkcolor := longint(cv);
- END;
-
- {------------------------------------------------------------------------------}
-
- PROCEDURE AppleStandardClut;
- VAR
- asc : CTabHandle;
- i : Integer;
- BEGIN
- IF GetMainDevice^^.gdPMap^^.Pixeltype = 1 THEN Exit(AppleStandardClut);
- asc := nil;
- asc := GetCTable(0);
- IF (asc = nil) THEN Exit(AppleStandardClut);
- for i := 0 to 255 do MyColorMap[i] := asc^^.ctTable[i];
- DisposCTable(asc);
- SetColorMap(MyColorMap);
- END;
-
- {------------------------------------------------------------------------------}
- {$ IMAGE }
- {------------------------------------------------------------------------------}
-
- PROCEDURE ReconstructImage;
- VAR
- onscreenrect,offscreenrect : Rect;
- viewwidth,viewheight,blastwidth,blastheight : Integer;
- temprect : Rect;
- BEGIN
- SetPort(viewwindow);
- viewwidth := viewwindow^.portrect.right - viewwindow^.portrect.left - 16;
- viewheight := viewwindow^.portrect.bottom - viewwindow^.portrect.top - 16;
- blastwidth := viewwidth;
- blastheight := viewheight;
- IF (viewwidth > documentwidth) then blastwidth := documentwidth;
- IF (viewheight > documentheight) THEN blastheight := documentheight;
- SetRect(onscreenrect,0,0,blastwidth,blastheight);
- SetRect(offscreenrect,0,0,blastwidth,blastheight);
- OffsetRect(offscreenrect,GetCtlValue(horizontalscroll),GetCtlValue(verticalscroll));
- SetColorMap(MyColorMap);
- Pennormal;
- SetForegroundColor(255);
- SetBackgroundColor(0);
- CopyBits( GrafPtr(docport)^.portBits, GrafPtr(viewwindow)^.portBits,
- offscreenrect,onscreenrect,srccopy,nil);
- SetRect(temprect,0,blastheight,viewwidth,viewheight);
- PenNormal;
- PenPat(gray);
- PaintRect(temprect);
- SetRect(temprect,blastwidth,0,viewwidth,viewheight);
- PaintRect(temprect);
- PenNormal;
- END;
-
- {------------------------------------------------------------------------------}
- {$S UTILITIES}
- {------------------------------------------------------------------------------}
-
- PROCEDURE ViewerError( errormessage : Str255 );
- VAR
- itemhit : Integer;
- BEGIN
- ParamText(errormessage,'','','');
- sysbeep(1);
- itemhit := NoteAlert(4000,nil);
- END;
-
- {------------------------------------------------------------------------------}
-
- PROCEDURE ResumeProc;
- BEGIN
- AppleStandardClut;
- ExitToShell;
- END;
-
-
- {------------------------------------------------------------------------------}
-
- FUNCTION ColorQDExists : Boolean;
- TYPE
- WordPtr = ^Integer;
- BEGIN
- ColorQDExists := BAND(WordPtr($28E)^,$C000) = 0;
- END;
-
- {------------------------------------------------------------------------------}
-
- FUNCTION DeviceHasEnoughColor : Boolean;
- BEGIN
- DeviceHasEnoughColor := GetMainDevice^^.gdPMap^^.pmtable^^.ctsize = 255;
- END;
-
- {------------------------------------------------------------------------------}
-
- FUNCTION RunningUnderMultiFinder : Boolean;
- CONST
- WNETrapNum = $60;
- UnImplTrapNum = $9F;
- BEGIN
- RunningUnderMultiFinder := GetTrapAddress(WNETrapNum) <> GetTrapAddress(UnImplTrapNum);
- END;
-
- {------------------------------------------------------------------------------}
-
- FUNCTION HorizontalFit ( somewindow : WindowPtr ) : Rect;
- VAR
- width,top : Integer;
- temprect : Rect;
- BEGIN
- width := somewindow^.portrect.right - somewindow^.portrect.left - 15;
- top := somewindow^.portrect.bottom - somewindow^.portrect.top - 15;
- SetRect(temprect,-1,top,width+1,top+16);
- HorizontalFit := temprect;
- END;
-
- {------------------------------------------------------------------------------}
-
- FUNCTION VerticalFit ( somewindow : WindowPtr ) : Rect;
- VAR
- height,left : Integer;
- temprect : Rect;
- BEGIN
- height := somewindow^.portrect.bottom - somewindow^.portrect.top - 15;
- left := somewindow^.portrect.right - somewindow^.portrect.left - 15;
- SetRect(temprect,left,-1,left+16,height+1);
- VerticalFit := temprect;
- END;
-
- {------------------------------------------------------------------------------}
-
- PROCEDURE AdjustScrollLimits;
- VAR
- horizontallimit,verticallimit : Integer;
- BEGIN
- horizontallimit := documentwidth - viewwindow^.portrect.right
- + viewwindow^.portrect.left + 16;
- verticallimit := documentheight - viewwindow^.portrect.bottom
- + viewwindow^.portrect.top + 16;
- IF horizontallimit < 0 THEN horizontallimit := 0;
- IF verticallimit < 0 THEN verticallimit := 0;
- SetCtlMax(horizontalscroll,horizontallimit);
- SetCtlMax(verticalscroll,verticallimit);
- ReconstructImage;
- END;
-
- {------------------------------------------------------------------------------}
- {$ INITIALIZE }
- {------------------------------------------------------------------------------}
-
- PROCEDURE SetupTheMac;
- BEGIN
- UnloadSeg(@_DataInit);
- InitGraf(@thePort);
- MoreMasters;
- MoreMasters;
- MoreMasters;
- MoreMasters;
- MoreMasters;
- MoreMasters;
- MoreMasters;
- MoreMasters;
- InitFonts;
- FlushEvents(everyEvent,0);
- InitWindows;
- InitMenus;
- TEInit;
- InitDialogs(@ResumeProc);
- MaxApplZone;
- InitCursor;
-
- watch := GetCursor(WatchCursor);
- SetCursor(watch^^);
- SetRect(draggingarea,4,24,screenBits.bounds.right-4,screenBits.bounds.bottom-4);
-
- IF NOT ColorQDExists THEN BEGIN
- ViewerError('You need a Mac II to run this program.');
- ExitToShell;
- END;
-
- IF NOT DeviceHasEnoughColor THEN BEGIN
- ViewerError('You need to have 256 colors available to run this program.');
- ExitToShell;
- END;
-
- END;
-
- {------------------------------------------------------------------------------}
-
- PROCEDURE SetupTheWindow;
- VAR
- temprect : Rect;
- BEGIN
- SetRect(temprect,50,50,500,400);
- viewwindow := NewCWindow(@viewwindowrecord,temprect,'Untitled',
- false,8,pointer(-1),true,0);
- horizontalscroll := NewControl(viewwindow,HorizontalFit(viewwindow),
- 'Horizontal',true,0,0,1,16,0);
- verticalscroll := NewControl(viewwindow,VerticalFit(viewwindow),
- 'Vertical',true,0,0,1,16,0);
- DrawGrowIcon(viewwindow);
- END;
-
- {------------------------------------------------------------------------------}
-
- PROCEDURE SetupTheMenus;
- BEGIN
- applemenu := NewMenu(1,'');
- filemenu := NewMenu(2,'File');
- editmenu := NewMenu(3,'Edit');
- AppendMenu(applemenu,'About PixelPaintâ„¢ Viewer...;(-');
- AppendMenu(filemenu,'View FullSize Doc.../O;View HalfSize Doc.../H;(Close/W;(-;Quit/Q');
- AppendMenu(editmenu,'Undo/Z;(-;Cut/X;Copy/C;Paste/V;Clear/B');
- AddResMenu(applemenu,'DRVR');
- InsertMenu(applemenu,0);
- InsertMenu(filemenu,0);
- InsertMenu(editmenu,0);
- DisableItem(editmenu,0);
- DrawMenuBar;
- END;
-
- {------------------------------------------------------------------------------}
-
- PROCEDURE FindTheMaxDocumentSize;
- VAR
- enoughroom : Ptr;
- BEGIN
- enoughroom := nil;
- enoughroom := NewPtr(1300000);
- IF (enoughroom <> nil) THEN BEGIN
- DisposPtr(enoughroom);
- documentwidth := 1024;
- documentheight := 1024;
- END
- ELSE BEGIN
- enoughroom := NewPtr(560000);
- IF (enoughroom <> nil) THEN BEGIN
- DisposPtr(enoughroom);
- documentwidth := 576;
- documentheight := 720;
- END
- ELSE BEGIN
- enoughroom := NewPtr(360000);
- IF (enoughroom <> nil) THEN BEGIN
- DisposPtr(enoughroom);
- documentwidth := 512;
- documentheight := 512;
- END
- ELSE BEGIN
- ViewerError('You don''t seem to have enough room to run this program.');
- ExitToShell;
- END;
- END;
- END;
- END;
-
-
- {------------------------------------------------------------------------------}
-
- PROCEDURE SetupTheDocument;
- VAR
- someport : GrafPtr;
- BEGIN
- FindTheMaxDocumentSize;
- IF NOT BuildNewPort(bufferport,bufferpix,1024,64,8) THEN BEGIN
- ViewerError('You don''t seem to have enough room to run this program.');
- ExitToShell;
- END;
- IF NOT BuildNewPort(docport,docpix, documentwidth, documentheight, 8) THEN BEGIN
- ViewerError('You don''t seem to have enough room to run this program.');
- ExitToShell;
- END;
- GetPort(someport);
- SetPort(GrafPtr(bufferport));
- AppleStandardClut;
- SetPort(GrafPtr(docport));
- PortSize(documentwidth,documentheight);
- AppleStandardClut;
- SetPort(someport);
- SetPt(docpt,0,0);
- SetPt(anchorpt,0,0);
- setport(viewwindow);
- AppleStandardClut;
- END;
-
- {------------------------------------------------------------------------------}
-
- PROCEDURE InitThings;
- BEGIN
- SetupTheMac;
- SetupTheWindow;
- SetupTheMenus;
- SetupTheDocument;
- InitCursor;
- END;
-
- {------------------------------------------------------------------------------}
- {$ FILE }
- {------------------------------------------------------------------------------}
-
- FUNCTION FileError( errorcode : Integer ) : Boolean;
- VAR
- fileerrormessage : Str255;
- BEGIN
- FileError := false;
- IF errorcode <> 0 THEN BEGIN
- NumToString(errorcode,fileerrormessage);
- fileerrormessage := Concat('File Error #',fileerrormessage,'... Cannot continue. ');
- ViewerError( fileerrormessage );
- FileError := true;
- END;
- END;
-
- {------------------------------------------------------------------------------}
-
- FUNCTION OpeningConversation( whattype : OSType; VAR reply : SFReply ) : Boolean;
- VAR
- typeslist : SFTypeList;
- somept : Point;
- BEGIN
- typeslist[0] := whattype;
- SetPt(somept,80,80);
- SFGetFile(somept,'Open...',nil,1,typeslist,nil,reply);
- OpeningConversation := reply.good;
- END;
-
- {------------------------------------------------------------------------------}
-
- FUNCTION SavingConversation( whattype : OSType; VAR reply : SFReply ) : Boolean;
- VAR
- typeslist : SFTypeList;
- somept : Point;
- name : Str255;
- BEGIN
- typeslist[0] := whattype;
- SetPt(somept,80,80);
- GetWTitle(viewwindow,name);
- SFPutFile(somept,'Save as...',name,nil,reply);
- SavingConversation := reply.good;
- END;
-
- {------------------------------------------------------------------------------}
-
- FUNCTION ReadPixelPaintHeader( thefilenumber : Integer ) : Boolean;
- LABEL
- 1;
- VAR
- texturebuffer,headerbuffer : Ptr;
- colorspace,headerspace,texturespace : LongInt;
- savedport : GrafPtr;
- BEGIN
- ReadPixelPaintHeader := false;
- headerbuffer := nil;
- texturebuffer := nil;
- headerspace := sizeof(FileHeader);
- colorspace := sizeof(ColorMapType);
- texturespace := sizeof(TextureMapType);
- purgemem(500000);
- headerbuffer := NewPtr(headerspace);
- texturebuffer := NewPtr(texturespace);
- IF (headerbuffer = nil) OR (texturebuffer = nil) THEN BEGIN
- ViewerError('Cannot allocate enough buffer space.');
- GOTO 1;
- END;
- IF FileError(SetFPos(thefilenumber,fsFromStart,0)) THEN GOTO 1;
- IF FileError(FSRead(thefilenumber,headerspace,headerbuffer)) THEN GOTO 1;
- IF FileHeaderPtr(headerbuffer)^.Version <> MaxInt THEN BEGIN
- ViewerError('This document cannot be read by this program.');
- GOTO 1;
- END;
- docsize := FileHeaderPtr(headerbuffer)^.CanvasSize;
- IF FileError(FSRead(thefilenumber,colorspace,@MyColorMap)) THEN GOTO 1;
- GetPort(savedport);
- SetPort(GrafPtr(bufferport));
- SetColorMap(MyColorMap);
- SetPort(GrafPtr(docport));
- SetColorMap(MyColorMap);
- SetPort(savedport);
- IF FileError(FSRead(thefilenumber,texturespace,texturebuffer)) THEN GOTO 1;
- ReadPixelPaintHeader := true;
- 1:
- IF (headerbuffer <> nil) THEN DisposPtr(Ptr(headerbuffer));
- IF (texturebuffer <> nil) THEN DisposPtr(texturebuffer);
- END;
-
- {------------------------------------------------------------------------------}
-
- FUNCTION GenericLoader(thefilenumber : Integer;
- thescreenrect : Rect;
- bottomofpict : Integer;
- rightofpict : Integer;
- scaleamt : Integer) : Boolean;
- LABEL
- 1;
- TYPE
- ByteBuffer = ARRAY[1..65] OF Char;
- VAR
- permdstptr, dstptr, srcptr : ptr;
- bufferrect, dstrect, trect :Rect;
- maxscanlines : integer;
- numbercompressed, count : longint;
- errCode, scanline, i : Integer;
- srcBuffer : ByteBuffer;
- aport : grafptr;
-
- FUNCTION OkayToBlast(x : Integer) : Boolean;
- BEGIN
- IF scaleamt = 1 THEN OkayToBlast := true
- ELSE IF (x mod scaleamt) = 0 THEN OkayToBlast := true
- ELSE OkayToBlast := false;
- END;
-
- BEGIN
- GetPort(aport);
- GenericLoader := false;
- permdstPtr := Ptr(bufferpix^^.baseaddr);
- SetRect(bufferrect, 0, 0, rightofpict, scaleamt);
- trect := thescreenrect;
- trect.bottom := trect.top + 1;
- PenNormal;
- SetForegroundColor(255);
- SetBackgroundColor(0);
- dstptr := permdstPtr;
- FOR scanLine := 1 TO bottomofpict DO BEGIN
- FOR i := 1 TO 16 DO BEGIN
- count := 4;
- IF FileError(FSRead(thefilenumber,count,@numbercompressed)) THEN GOTO 1;
- IF FileError(FSRead(thefilenumber,numbercompressed,@srcbuffer)) THEN GOTO 1;
- srcptr := @srcbuffer;
- UnPackBits(srcptr,dstptr,64);
- END;
- IF OkayToBlast(scanline) THEN BEGIN
- dstrect := trect;
- offsetrect(dstrect, 0, (scanline div scaleamt) - 1);
- CopyBits(grafptr(bufferport)^.portBits,aport^.portBits,
- bufferrect,dstrect,srccopy,nil);
- dstptr := permdstPtr;
- END;
- end;
- GenericLoader := true;
- 1:
- END;
-
- {------------------------------------------------------------------------------}
-
- FUNCTION ReadPixelPaintDoc( thefilenumber : Integer; Scaling : Integer ) : Boolean;
- VAR
- x,y : Integer;
- myrect : Rect;
- BEGIN
- CASE docsize OF
- 1: BEGIN x := 576; y := 720; END;
- 2: BEGIN x := 512; y := 512; END;
- 3: BEGIN x := 1024; y := 1024; END;
- 4: BEGIN x := 1024; y := 768; END;
- END;
- SetRect(myrect,0,0,(x div scaling),y);
- SetPort(GrafPtr(docport));
- SetForegroundColor(255);
- SetBackgroundColor(0);
- PenNormal;
- PenPat(ltgray);
- PaintRect(docport^.portrect);
- PenNormal;
- ReadPixelPaintDoc := GenericLoader(thefilenumber,myrect,y,x,Scaling);
- END;
-
- {------------------------------------------------------------------------------}
-
- PROCEDURE DoOpenPixelPaint( scaling : Integer );
- VAR
- answer : SFReply;
- headerisokay,docisokay : Boolean;
- thefilenumber : Integer;
- oldvolref : Integer;
- BEGIN
- headerisokay := false;
- docisokay := false;
- IF NOT OpeningConversation('PX01',answer) THEN Exit(DoOpenPixelPaint);
- SetCursor(watch^^);
- IF FileError( GetVol(nil,oldvolref) ) THEN Exit(DoOpenPixelPaint);
- IF FileError( SetVol(nil,answer.vrefnum) ) THEN Exit(DoOpenPixelPaint);
- IF FileError( FSOpen(answer.fname,answer.vrefnum,thefilenumber) ) THEN Exit(DoOpenPixelPaint);
- headerisokay := ReadPixelPaintHeader(thefilenumber);
- IF headerisokay THEN
- docisokay := ReadPixelPaintDoc(thefilenumber,scaling);
- IF FileError( FSClose(thefilenumber) ) THEN Exit(DoOpenPixelPaint);
- IF FileError( SetVol(nil,oldvolref) ) THEN Exit(DoOpenPixelPaint);
- IF (NOT headerisokay) OR (NOT docisokay) THEN Exit(DoOpenPixelPaint);
- ShowWindow(viewwindow);
- SelectWindow(viewwindow);
- DisableItem(filemenu,1);
- DisableItem(filemenu,2);
- EnableItem(filemenu,3);
- DisableItem(editmenu,0);
- DrawMenuBar;
- END;
-
- {------------------------------------------------------------------------------}
- {$ EVENT }
- {------------------------------------------------------------------------------}
-
- FUNCTION ThereIsAnEvent( eventmask : Integer; VAR theevent : EventRecord ) : Boolean;
- BEGIN
- IF RunningUnderMultiFinder THEN
- ThereIsAnEvent := WaitNextEvent( eventmask, theevent, 5, nil )
- ELSE
- ThereIsAnEvent := GetNextEvent( eventmask, theevent );
- END;
-
- {------------------------------------------------------------------------------}
-
- PROCEDURE DoAbout;
- VAR
- thedialog : DialogPtr;
- itemhit : Integer;
- BEGIN
- thedialog := GetNewDialog( 3000, nil, POINTER(-1) );
- IF thedialog <> nil THEN BEGIN
- ModalDialog( nil, itemhit );
- CloseDialog( thedialog );
- END;
- END;
-
- {------------------------------------------------------------------------------}
-
- PROCEDURE DoDeskAccessory( theitem : Integer );
- VAR
- name : Str255;
- temp : Integer;
- BEGIN
- GetItem(applemenu,theitem,name);
- temp := OpenDeskAcc(name);
- SetPort(viewwindow);
- EnableItem(editmenu,0);
- DrawMenuBar;
- END;
-
- {------------------------------------------------------------------------------}
-
- PROCEDURE HandleAppleMenu( theitem : Integer );
- BEGIN
- IF theitem = 1 THEN DoAbout
- ELSE DoDeskAccessory( theitem );
- END;
-
- {------------------------------------------------------------------------------}
-
- PROCEDURE DoClose;
- BEGIN
- HideWindow(viewwindow);
- EnableItem(filemenu,1);
- EnableItem(filemenu,2);
- DisableItem(filemenu,3);
- IF FrontWindow = nil THEN DisableItem(editmenu,0)
- ELSE EnableItem(editmenu,0);
- DrawMenuBar;
- END;
-
- {------------------------------------------------------------------------------}
-
- PROCEDURE DoQuit;
- BEGIN
- userwantstoquit := true;
- END;
-
- {------------------------------------------------------------------------------}
-
- PROCEDURE HandleFileMenu( theitem : Integer );
- BEGIN
- CASE theitem OF
- 1 : DoOpenPixelPaint(1);
- 2 : DoOpenPixelPaint(2);
- 3 : DoClose;
- 5 : DoQuit;
- END;
- END;
-
- {------------------------------------------------------------------------------}
-
- PROCEDURE HandleEditMenu( theitem : Integer );
- VAR
- dummy : Boolean;
- BEGIN
- IF viewwindow = FrontWindow THEN Exit(HandleEditMenu);
- dummy := SystemEdit( theitem-1 );
- END;
-
- {------------------------------------------------------------------------------}
-
- PROCEDURE HandleTheMenu( menuresult : LongInt );
- VAR
- themenu,theitem : Integer;
- BEGIN
- themenu := HiWord( menuresult );
- theitem := LoWord( menuresult );
- CASE themenu OF
- 1 : HandleAppleMenu(theitem);
- 2 : HandleFileMenu(theitem);
- 3 : HandleEditMenu(theitem);
- END;
- HiliteMenu(0);
- END;
-
- {------------------------------------------------------------------------------}
-
- FUNCTION IsItFront( somewindow : WindowPtr ) : Boolean;
- BEGIN
- IF somewindow <> FrontWindow THEN BEGIN
- SelectWindow(somewindow);
- IF somewindow <> viewwindow THEN EnableItem(editmenu,0)
- ELSE DisableItem(editmenu,0);
- DrawMenuBar;
- IsItFront := false;
- END
- ELSE IsItFront := true;
- END;
-
- {------------------------------------------------------------------------------}
-
- FUNCTION IsItOurs( somewindow : WindowPtr ) : Boolean;
- BEGIN
- IsItOurs := somewindow = viewwindow;
- END;
-
- {------------------------------------------------------------------------------}
-
- FUNCTION ClickedInScrollBar( somept : Point ) : Boolean;
- BEGIN
- SetPort(viewwindow);
- GlobalToLocal(somept);
- IF (somept.v > viewwindow^.portrect.bottom-16) OR
- (somept.h > viewwindow^.portrect.right-16) THEN ClickedInScrollBar := true
- ELSE ClickedInScrollBar := false;
- END;
-
- {------------------------------------------------------------------------------}
-
- PROCEDURE ScrollIt( theControl : ControlHandle; thePart : Integer );
- VAR
- delta,oldvalue : Integer;
- BEGIN
- IF thePart = 0 THEN Exit(ScrollIt);
- CASE thePart OF
- inUpButton : delta := -1;
- inDownButton : delta := 1;
- inPageUp : delta := -24;
- inPageDown : delta := 24;
- END;
- SetCtlValue(theControl, delta + GetCtlValue(theControl));
- ReconstructImage;
- END;
-
- {------------------------------------------------------------------------------}
-
- PROCEDURE HandleScrolling( theevent : EventRecord );
- VAR
- whichcontrol : ControlHandle;
- thepart : Integer;
- BEGIN
- SetPort(viewwindow);
- GlobalToLocal(theevent.where);
- thepart := FindControl( theevent.where, viewwindow, whichcontrol );
- IF thepart = 0 THEN Exit(HandleScrolling);
- IF whichcontrol = nil THEN Exit(HandleScrolling);
- IF thepart = inThumb THEN
- thepart := TrackControl(whichcontrol,theevent.where,nil)
- ELSE
- thepart := TrackControl(whichcontrol,theevent.where,@ScrollIt);
- ReconstructImage;
- END;
-
- {------------------------------------------------------------------------------}
-
- PROCEDURE HandleContentClick( theevent : EventRecord; somewindow : WindowPtr );
- BEGIN
- IF NOT IsItFront(somewindow) THEN Exit(HandleContentClick);
- IF NOT IsItOurs(somewindow) THEN Exit(HandleContentClick);
- IF ClickedInScrollBar( theevent.where ) THEN HandleScrolling( theevent );
- END;
-
- {------------------------------------------------------------------------------}
-
- PROCEDURE HandleGrow( theevent : EventRecord; somewindow : WindowPtr );
- VAR
- limits : Rect;
- result : LongInt;
- vertrect,horzrect : Rect;
- BEGIN
- IF NOT IsItFront(somewindow) THEN Exit(HandleGrow);
- IF NOT IsItOurs(somewindow) THEN Exit(HandleGrow);
- SetRect(limits,100,100,draggingarea.right-draggingarea.left,draggingarea.bottom-draggingarea.top);
- result := GrowWindow( somewindow, theevent.where, limits );
- SetPort(somewindow);
- EraseRect(somewindow^.portrect);
- SizeWindow(somewindow,LoWord(result),HiWord(result),true);
- vertrect := VerticalFit(somewindow);
- horzrect := HorizontalFit(somewindow);
- MoveControl(verticalscroll,vertrect.left,vertrect.top);
- SizeControl(verticalscroll,vertrect.right-vertrect.left,vertrect.bottom-vertrect.top);
- MoveControl(horizontalscroll,horzrect.left,horzrect.top);
- SizeControl(horizontalscroll,horzrect.right-horzrect.left,horzrect.bottom-horzrect.top);
- END;
-
- {------------------------------------------------------------------------------}
-
- PROCEDURE HandleGoAway( theevent : EventRecord; somewindow : WindowPtr );
- BEGIN
- IF NOT IsItFront(somewindow) THEN Exit(HandleGoAway);
- IF NOT IsItOurs(somewindow) THEN Exit(HandleGoAway);
- IF TrackGoAway(somewindow,theevent.where) THEN BEGIN
- DoClose;
- END;
- END;
-
- {------------------------------------------------------------------------------}
-
- PROCEDURE HandleDrag( theevent : EventRecord; somewindow : WindowPtr );
- BEGIN
- IF NOT IsItFront(somewindow) THEN Exit(HandleDrag);
- IF NOT IsItOurs(somewindow) THEN Exit(HandleDrag);
- DragWindow(somewindow,theevent.where,draggingarea);
- SetPort(somewindow);
- DrawControls(somewindow);
- DrawGrowIcon(somewindow);
- END;
-
- {------------------------------------------------------------------------------}
-
- PROCEDURE HandleZoom( partcode : integer; theevent : EventRecord; somewindow : WindowPtr );
- VAR
- vertrect,horzrect : Rect;
- BEGIN
- IF NOT IsItFront(somewindow) THEN Exit(HandleZoom);
- IF NOT IsItOurs(somewindow) THEN Exit(HandleZoom);
- IF TrackBox(somewindow, theevent.where, partcode) THEN BEGIN
- SetPort(somewindow);
- EraseRect(somewindow^.portrect);
- ZoomWindow(somewindow,partcode,true);
- vertrect := VerticalFit(somewindow);
- horzrect := HorizontalFit(somewindow);
- MoveControl(verticalscroll,vertrect.left,vertrect.top);
- SizeControl(verticalscroll,vertrect.right-vertrect.left,vertrect.bottom-vertrect.top);
- MoveControl(horizontalscroll,horzrect.left,horzrect.top);
- SizeControl(horizontalscroll,horzrect.right-horzrect.left,horzrect.bottom-horzrect.top);
- END;
- END;
-
- {------------------------------------------------------------------------------}
-
- PROCEDURE HandleTheSystem( theevent : EventRecord; somewindow : WindowPtr );
- BEGIN
- SystemClick(theevent,somewindow);
- IF FrontWindow = nil THEN DisableItem(editmenu,0)
- ELSE IF FrontWindow = viewwindow THEN DisableItem(editmenu,0)
- ELSE EnableItem(editmenu,0);
- DrawMenuBar;
- END;
-
- {------------------------------------------------------------------------------}
-
- PROCEDURE HandleTheMouse( theevent : EventRecord );
- VAR
- somewindow : WindowPtr;
- BEGIN
- CASE FindWindow( theevent.where, somewindow ) OF
- inSysWindow : HandleTheSystem(theevent,somewindow);
- inMenuBar : HandleTheMenu( MenuSelect(theevent.where) );
- inDrag : HandleDrag(theevent,somewindow);
- inContent : HandleContentClick(theevent,somewindow);
- inGrow : HandleGrow(theevent,somewindow);
- inGoAway : HandleGoAway(theevent,somewindow);
- inZoomIn : HandleZoom(inZoomIn,theevent,somewindow);
- inZoomOut : HandleZoom(inZoomOut,theevent,somewindow);
- END;
- END;
-
- {------------------------------------------------------------------------------}
-
- PROCEDURE HandleTheKeyboard( theevent : EventRecord );
- BEGIN
- IF BAND(theevent.modifiers,cmdkey) = cmdkey THEN
- HandleTheMenu( MenuKey( CHR(BAND(theevent.message,charcodemask)) ) );
- END;
-
- {------------------------------------------------------------------------------}
-
- PROCEDURE HandleTheActivate( theevent : EventRecord );
- BEGIN
- IF NOT IsItOurs(WindowPtr(theevent.message)) THEN Exit(HandleTheActivate);
- IF BAND(theevent.modifiers,activeFlag) <> 0 THEN BEGIN
- HiliteControl( horizontalscroll, 0 );
- HiliteControl( verticalscroll, 0 );
- DisableItem(editmenu,0);
- END
- ELSE BEGIN
- HiliteControl( horizontalscroll, 255 );
- HiliteControl( verticalscroll, 255 );
- EnableItem(editmenu,0);
- END;
- DrawControls(viewwindow);
- DrawGrowIcon(viewwindow);
- DrawMenuBar;
- END;
-
- {------------------------------------------------------------------------------}
-
- PROCEDURE HandleTheUpdate( theevent : EventRecord );
- BEGIN
- IF NOT IsItOurs(WindowPtr(theevent.message)) THEN Exit(HandleTheUpdate);
- SetPort(viewwindow);
- BeginUpdate(viewwindow);
- EraseRect(viewwindow^.portrect);
- EndUpdate(viewwindow);
- AdjustScrollLimits;
- DrawControls(viewwindow);
- DrawGrowIcon(viewwindow);
- END;
-
- {------------------------------------------------------------------------------}
-
- PROCEDURE HandleTheEvent( theevent : EventRecord );
- BEGIN
- CASE theevent.what OF
- mouseDown : HandleTheMouse( theevent );
- keyDown,autoKey : HandleTheKeyboard( theevent );
- activateEvt : HandleTheActivate( theevent );
- updateEvt : HandleTheUpdate( theevent );
- OTHERWISE { nothing } ;
- END;
- END;
-
- {------------------------------------------------------------------------------}
- {$ MAIN }
- {------------------------------------------------------------------------------}
-
- PROCEDURE MainEventLoop;
- BEGIN
- userwantstoquit := false;
- REPEAT
- SystemTask;
- InitCursor;
- IF ThereIsAnEvent( everyevent, viewevent ) THEN HandleTheEvent( viewevent );
- UNTIL userwantstoquit;
- END;
-
- {------------------------ the main program starts here -------------------------}
-
- BEGIN
-
- InitThings;
- UnloadSeg(@InitThings);
- MainEventLoop;
- ResumeProc;
-
- END.